package com.sq.partner.manager.service.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.ICoreCoverageDao;
import com.sq.partner.manager.dao.IGatewayCorePlanCovDao;
import com.sq.partner.manager.dao.IGatewayCoreProdPlanDao;
import com.sq.partner.manager.dao.ITradFeeAdjustConditionDao;
import com.sq.partner.manager.dao.ITradProdInstDao;
import com.sq.partner.manager.dao.IGatewayRiskAlgMapDao;
import com.sq.partner.manager.dao.IGatewayTradCovDao;
import com.sq.partner.manager.dao.IGatewayTradInsuPlanDao;
import com.sq.partner.manager.dao.IGatewayTradPlanBaseDao;
import com.sq.partner.manager.dao.IGatewayTradPlanCovDao;
import com.sq.partner.manager.dao.IGatewayTradProdPlanDao;
import com.sq.partner.manager.mybatis.model.CoreCoverage;
import com.sq.partner.manager.mybatis.model.CorePlanCov;
import com.sq.partner.manager.mybatis.model.CoreProdInst;
import com.sq.partner.manager.mybatis.model.CoreProdPlan;
import com.sq.partner.manager.mybatis.model.RiskAlgMap;
import com.sq.partner.manager.mybatis.model.TradCalDef;
import com.sq.partner.manager.mybatis.model.TradCov;
import com.sq.partner.manager.mybatis.model.TradFeeAdjustCondition;
import com.sq.partner.manager.mybatis.model.TradInsuPlan;
import com.sq.partner.manager.mybatis.model.TradItemCal;
import com.sq.partner.manager.mybatis.model.TradPlanBase;
import com.sq.partner.manager.mybatis.model.TradPlanCov;
import com.sq.partner.manager.mybatis.model.TradProdInst;
import com.sq.partner.manager.mybatis.model.TradProdPlan;
import com.sq.partner.manager.service.IGatewayClearCacheManager;
import com.sq.partner.manager.service.IGatewayChannelInfoConfigService;
import com.sq.partner.manager.service.IGatewayProductService;
import com.sq.partner.manager.util.JsonUtils;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@Service
public class GatewayProductServiceImpl extends BaseServiceImpl<TradProdInst, String> implements
		IGatewayProductService {
	
	@Resource
	ITradProdInstDao tradProdInstDao;
	@Resource
	ITradFeeAdjustConditionDao tradFeeAdjustConditionDao;
	@Resource
	IGatewayCoreProdPlanDao gatewayCoreProdPlanDao;
	@Resource
	IGatewayCorePlanCovDao gatewayCorePlanCovDao;
	@Resource
	IGatewayTradPlanBaseDao gatewayTradPlanBaseDao;
	@Resource
	IGatewayRiskAlgMapDao gatewayRiskAlgMapDao;
	@Resource
	IGatewayTradProdPlanDao gatewayTradProdPlanDao;
	@Resource
	IGatewayTradPlanCovDao gatewayTradPlanCovDao;
	@Resource
	IGatewayTradCovDao gatewayTradCovDao;
	@Resource
	IGatewayTradInsuPlanDao gatewayTradInsuPlanDao;
	
	@Resource
	IGatewayClearCacheManager gatewayClearCacheManager;
	
	@Resource
	ICoreCoverageDao coreCoverageDao;
	@Resource
	IGatewayChannelInfoConfigService gatewayChannelInfoConfigService;
	
	public Page<TradProdInst> queryPage(Page<TradProdInst> page) throws SQLException {
		// TODO Auto-generated method stub
		return tradProdInstDao.queryPage(page);
	}

	@Override
	public TradProdInst queryTradProdInst(TradProdInst tradProdInst)
			throws SQLException {
		tradProdInst = tradProdInstDao.queryTradProdInst(tradProdInst);
		List<TradFeeAdjustCondition> tradFeeAdjustConditions = tradFeeAdjustConditionDao.queryTradFeeAdjustConditionsByTradProdCode(tradProdInst);
		String adjustStr = "";
		if(tradFeeAdjustConditions!=null&&!tradFeeAdjustConditions.isEmpty()){
			String adjustId = tradFeeAdjustConditions.get(0).getAdjustId();
			JsonObject jo = new JsonObject();
			jo.addProperty("adjustId", adjustId);
			JsonParser jp=new JsonParser();
			JsonElement je =jp.parse(JsonUtils.toJson(tradFeeAdjustConditions));
			jo.add("adjustList", je);
			adjustStr=JsonUtils.toJson(jo);
		}
		tradProdInst.setAdjustStr(adjustStr);
		return tradProdInst;
	}
	public Page<CoreProdInst> queryProdInstPage(Page<CoreProdInst> page) throws SQLException {
		// TODO Auto-generated method stub
		return tradProdInstDao.queryProdInstPage(page);
	}
	
	public TradProdInst  saveTradProdInst(TradProdInst tradProdInst) throws Exception {
		TradProdInst rtradProdInst =tradProdInstDao.findMaxTradProdCode(tradProdInst);
		
		if(null!=rtradProdInst&&StrUtil.isNotEmpty(rtradProdInst.getTradProdCode())){
			long tradProdCode= Long.parseLong(rtradProdInst.getTradProdCode())+1;
			String nextProdCodeVal = String.format("%03d", tradProdCode);
			tradProdInst.setTradProdCode(nextProdCodeVal);

		}else{
			long tradProdCode=Long.parseLong(tradProdInst.getProdInstCode());
			tradProdInst.setTradProdCode(tradProdCode+"001");
		}
		Date sysdate = new Date();
		tradProdInst.setStartDate(sysdate);
		tradProdInstDao.saveTradProdInst(tradProdInst);//保存产品
		if(tradProdInst.getAdjustStr()!=null&&tradProdInst.getAdjustStr().length()>0){
			JsonObject o = new JsonParser().parse(tradProdInst.getAdjustStr()).getAsJsonObject();
			String adjustId= o.get("adjustId").getAsString();
			JsonArray jary = o.get("adjustList").getAsJsonArray();
			TradFeeAdjustCondition tradFeeAdjustCondition = null;
			if(adjustId!=null&&!"".equals(adjustId)){
				for (JsonElement jsonElement : jary) {
					String peoplenum =jsonElement.getAsJsonObject().get("peoplenum").getAsString();
					String coefficient=jsonElement.getAsJsonObject().get("coefficient").getAsString();
					if(!StrUtil.isEmpty(peoplenum)&&!StrUtil.isEmpty(coefficient)){
						BigDecimal premRate = BigDecimal.valueOf(Double.parseDouble(coefficient)/100);
						tradFeeAdjustCondition = new TradFeeAdjustCondition();
						tradFeeAdjustCondition.setAdjustId(adjustId);
						tradFeeAdjustCondition.setTradProdCode(tradProdInst.getTradProdCode());
						tradFeeAdjustCondition.setPremRate(premRate.setScale(2, BigDecimal.ROUND_HALF_UP));
						tradFeeAdjustCondition.setCond1(Long.parseLong(peoplenum));
						tradFeeAdjustConditionDao.saveTradFeeAdjustCondition(tradFeeAdjustCondition);
					}
				}
			}
		}
		return tradProdInst;
	}
	

	@Override
	public TradProdInst queryTradProdInstById(String tradProdCode)
			throws SQLException {
		// TODO Auto-generated method stub
		return tradProdInstDao.queryTradProdInstById(tradProdCode);
	}
	@Override
	public Page<CoreProdPlan> queryProdPlan(Page<CoreProdPlan> page,TradProdInst tradProdInst)
			throws SQLException {
		return tradProdInstDao.queryProdPlanPage(page,tradProdInst);
	}
	@Override
	public Page<CoreProdPlan> queryProdPlan(String tradProdCode) throws SQLException {
		// TODO Auto-generated method stub
		Page<CoreProdPlan> page =new Page<CoreProdPlan>();
		page.setData(tradProdInstDao.queryProdPlan(tradProdCode));
		return page;
	}
	@Override
	public List<TradCalDef> queryTradCalDef(TradCalDef trad) throws SQLException {
		// TODO Auto-generated method stub
		return tradProdInstDao.queryTradCalDef(trad);
	}
	@Override
	public void savePlan(CoreProdPlan choosePlan,String tradProdCode, String prodInstCode,
			String insType, String planCode, String instCode, String insnme,
			String tradPlanDesc,BigDecimal amount, String required, String calDefId,BigDecimal poolRatio,
			String calId,String isHealthFlag,String userCode) throws Exception {
		
			
			//根据传入的planCode查出  CoreProdPlan
			// 交易系统险种表
			TradPlanBase tradplanbase = new TradPlanBase();
			
			tradplanbase.setAmount(amount);
			tradplanbase.setInstCod(instCode);
			tradplanbase.setPlanCode(planCode);
			tradplanbase.setTradPlanDesc(tradPlanDesc);
			tradplanbase.setPoolRatio(poolRatio);
			//用PLAN_CODE在TradPlanBase里找到最大数据的id
			String tradPlanCode = "";
			TradPlanBase stradplanbase = gatewayTradPlanBaseDao.findMaxTradPlanCodebyPlanCode(tradplanbase);
			if(null!=stradplanbase&&StrUtil.isNotEmpty(stradplanbase.getTradPlanCode())){
				long stradPlanCode= Long.parseLong(stradplanbase.getTradPlanCode())+1;
				String nextPlanCodeVal = String.format("%03d", stradPlanCode);
				tradPlanCode = nextPlanCodeVal;
			}else{
				long stradPlanCode=Integer.parseInt(tradplanbase.getPlanCode());
				tradPlanCode=stradPlanCode+"001";
			}
			
			
			tradplanbase.setTradPlanCode(tradPlanCode);
			gatewayTradPlanBaseDao.savetradplanbase(tradplanbase);		
			
			// 新增 交易系统产品险种关联
			TradProdPlan tradprodplan = new TradProdPlan();
			
			tradprodplan.setInsType(insType);
			tradprodplan.setRequired(required);
			tradprodplan.setTradPlanCode(tradPlanCode);
			tradprodplan.setTradProdCode(tradProdCode);
			// 将险种下的责任放入到交易系统现险种下
			List<CorePlanCov> finPlanCov = null;//this.getProductDefinitBM().findPlanCov(PLAN_CODE, inst_code);
			CorePlanCov corePlanCov=new CorePlanCov();
			corePlanCov.setInstCode(instCode);
			corePlanCov.setPlanCode(planCode);
			finPlanCov= gatewayCorePlanCovDao.queryCorePlanCov(corePlanCov);
		
			List<TradPlanCov> tradplancovList = new ArrayList<TradPlanCov>();
			TradPlanCov tradPlanCov=null;
			TradCov tradcov = null;
			List<TradCov> tradcovList = new ArrayList<TradCov>();
			
			RiskAlgMap riskAlgMap=new RiskAlgMap();
			for (CorePlanCov PlanCov : finPlanCov) {
				
				tradPlanCov=new TradPlanCov();
				tradcov= new TradCov();
				
				tradPlanCov.setTradPlanCode(tradPlanCode);
				String tradCovCode="";
				TradCov sTradCov=gatewayTradCovDao.findMaxTradCovCodebyCovCode(PlanCov.getCovcode());
				
				if(null!=sTradCov&&StrUtil.isNotEmpty(sTradCov.getTradCovCode())){
					long sTradCovCode= Long.parseLong(sTradCov.getTradCovCode())+1;
					String nextCovCodeVal = String.format("%03d", sTradCovCode);
					tradCovCode=nextCovCodeVal;
				}else{
					long sTradCovCode=Integer.parseInt(PlanCov.getCovcode());
					tradCovCode=sTradCovCode+"001";
				}
				tradPlanCov.setTradCovCode(tradCovCode);
				tradplancovList.add(tradPlanCov);

				tradcov.setTradCovCode(tradCovCode);
				tradcov.setCovCode(PlanCov.getCovcode());
				tradcov.setTradCovName(PlanCov.getCovnme());
				tradcovList.add(tradcov);
				
			}
			riskAlgMap.setTradPlanCode(tradPlanCode);
			riskAlgMap.setCalDefId(calDefId);
			riskAlgMap.setCalId(calId);
			riskAlgMap.setOperatorsName(userCode);
			riskAlgMap.setOperatorTime(new Date());
			gatewayRiskAlgMapDao.saveRiskAlgMap(riskAlgMap);		//险种算法映射表
			
			gatewayTradProdPlanDao.saveTradProdPlan(tradprodplan);	//添加产品&险种关联
			
//			gatewayTradPlanCovDao.saveTradPlanCovList(tradplancovList);	//添加交易系统险种&责任关联
			
//			gatewayTradCovDao.saveTradCovList(tradcovList);			//维护责任表
			//added by zzGuo@20160120 for 添加人险产品健康险标识处理 begin
			if("Y".equalsIgnoreCase(isHealthFlag)){
				TradProdInst queryResult = tradProdInstDao.queryTradProdInstById(tradProdCode);
				if(null!=queryResult && !"Y".equalsIgnoreCase(queryResult.getIsHealthFlag())){
					TradProdInst temObj = new TradProdInst();
					temObj.setTradProdCode(tradProdCode);
					temObj.setIsHealthFlag("Y");
					tradProdInstDao.updateTradProdInstIsHealthFlag(temObj);
				}
			}
			//added by zzGuo@20160120 for 添加人险产品健康险标识处理 end
			//添加险种时判断产品是否有关联投保方案：清缓存
			List<TradInsuPlan> tradInsuPlanLists=gatewayTradInsuPlanDao.queryTradInsuPlanByProdCode(tradProdCode);
			if(null!=tradInsuPlanLists&&tradInsuPlanLists.size()>0){
				gatewayChannelInfoConfigService.clearAllCacheByChannelCodeExtend(tradInsuPlanLists);
			}
	}
	@Override
	public CoreProdPlan queryCoreProdPlan(String planCode) throws SQLException {
		
		CoreProdPlan coreProdPlan=new CoreProdPlan();
		coreProdPlan.setPlanCode(planCode);
		return gatewayCoreProdPlanDao.queryCoreProdPlan(coreProdPlan);
	}
	@Override
	public Page<TradPlanBase> queryTradPlanBasePage(Page<TradPlanBase> page,
			String tradProdCode) throws SQLException {
		return gatewayTradPlanBaseDao.queryTradPlanBasePage(page,tradProdCode);
	}
	@Override
	public Page<TradPlanCov> queryCoveragePage(Page<TradPlanCov> page)
			throws SQLException {
		
		return gatewayTradPlanCovDao.queryCoveragePage(page);
	}
	@Override
	public Page<TradInsuPlan> queryTradInsuPlanPage(Page<TradInsuPlan> page) throws SQLException {
		// TODO Auto-generated method stub
		return gatewayTradInsuPlanDao.queryTradInsuPlanPage(page);
	}
	@Override
	public void saveCov(List<TradPlanCov> planCovs) throws Exception {
		
			DecimalFormat decimalFormat = new DecimalFormat("#.00");
			//planCovs.get(0);
			/* 一、责任保额不为空，校验格式通过后，判断责任代码是否为空：
			*1、责任代码为空：根据前台传入的责任模板代码，获取最大的代码+1即得到新的责任代码，进行保存。
			*2、责任代码不为空：根据责任代码、交易系统险种代码更新险种责任关联表对应的保额字段
			* 二、责任保额为空：判断责任代码是否为空
			*1、责任代码不为空：删除该责任与对应险种的关联数据
			*2、责任代码为空：无操作。
			**/
			for(TradPlanCov tradPlanCov:planCovs){

				if(null!=tradPlanCov.getCovAmt()){
					BigDecimal formatAmt=new BigDecimal(decimalFormat.format(tradPlanCov.getCovAmt()));
					//获取下一个责任代码
					String tradCovCode = "";
					if(StrUtil.isEmpty(tradPlanCov.getTradCovCode())){
						TradCov sTradCov=gatewayTradCovDao.findMaxTradCovCodebyCovCode(tradPlanCov.getCoreCovCode());
						if(null!=sTradCov&&StrUtil.isNotEmpty(sTradCov.getTradCovCode())){
							long sTradCovCode= Long.parseLong(sTradCov.getTradCovCode())+1;
							String nextCovCodeVal = String.format("%03d", sTradCovCode);
							tradCovCode=nextCovCodeVal;
						}else{
							long sTradCovCode=Integer.parseInt(tradPlanCov.getCoreCovCode());
							tradCovCode=sTradCovCode+"001";
						}
					
						TradPlanCov tp = new TradPlanCov();
						tp.setTradPlanCode(tradPlanCov.getTradPlanCode());
						tp.setTradCovCode(tradCovCode);
						tp.setCovAmt(formatAmt);
						tp.setIsInputAmount(tradPlanCov.getIsInputAmount());
						tp.setNoCalimAmount(tradPlanCov.getNoCalimAmount());
						if(null!=tradPlanCov.getClaimRate()){
							tp.setClaimRate(tradPlanCov.getClaimRate().divide(new BigDecimal(100)).setScale(5,BigDecimal.ROUND_HALF_UP));
						}
						List<TradPlanCov> pclist = new ArrayList<TradPlanCov>();
						pclist.add(tp);
						gatewayTradPlanCovDao.saveTradPlanCovList(pclist);//新增
						

						//根据传入的责任模板代码，查询对应的责任模板名称
						CoreCoverage cc = coreCoverageDao.queryCoreCoverageByCode(tradPlanCov.getCoreCovCode());	
						List<TradCov> covlist = new ArrayList<TradCov>();
						TradCov tc = new TradCov();
						tc.setCovCode(tradPlanCov.getCoreCovCode());
						tc.setTradCovCode(tradCovCode);
						tc.setTradCovName(cc.getCovnme());

						covlist.add(tc);
						
						gatewayTradCovDao.saveTradCovList(covlist);	//维护责任表
					}else{
						TradPlanCov tpc = new TradPlanCov();
						tpc.setTradCovCode(tradPlanCov.getTradCovCode());
						tpc.setTradPlanCode(tradPlanCov.getTradPlanCode());
						tpc.setCovAmt(formatAmt);
						tpc.setIsInputAmount(tradPlanCov.getIsInputAmount());
						if(null!=tradPlanCov.getClaimRate()){
							tpc.setClaimRate(tradPlanCov.getClaimRate().divide(new BigDecimal(100)).setScale(5,BigDecimal.ROUND_HALF_UP));
						}
						tpc.setNoCalimAmount(tradPlanCov.getNoCalimAmount());
						gatewayTradPlanCovDao.updateTradPlanCov(tpc);//修改
					}
				}else{
					if(StrUtil.isNotEmpty(tradPlanCov.getTradCovCode())){
						if("Y".equals(tradPlanCov.getIsInputAmount())){
							TradPlanCov tpc2 = new TradPlanCov();
							tpc2.setTradCovCode(tradPlanCov.getTradCovCode());
							tpc2.setTradPlanCode(tradPlanCov.getTradPlanCode());
							gatewayTradPlanCovDao.deleteTradPlanCov(tpc2);
							
							TradCov tc = new TradCov();
							String tradCovCode = tradPlanCov.getTradCovCode();
//						tc.setCovCode(tradCovCode);
							gatewayTradCovDao.deleteTradCovByid(tradCovCode);//删除责任表数据
						}else{
							TradPlanCov tpc = new TradPlanCov();
							tpc.setTradCovCode(tradPlanCov.getTradCovCode());
							tpc.setTradPlanCode(tradPlanCov.getTradPlanCode());
							tpc.setIsInputAmount(tradPlanCov.getIsInputAmount());
							if(null!=tradPlanCov.getClaimRate()){
								tpc.setClaimRate(tradPlanCov.getClaimRate().divide(new BigDecimal(100)).setScale(5,BigDecimal.ROUND_HALF_UP));
							}
							tpc.setNoCalimAmount(tradPlanCov.getNoCalimAmount());
							gatewayTradPlanCovDao.updateTradPlanCov(tpc);//修改

						}
					}else{
						if("N".equals(tradPlanCov.getIsInputAmount())){
							String tempCode = "";
							
							TradCov sTradCov=gatewayTradCovDao.findMaxTradCovCodebyCovCode(tradPlanCov.getCoreCovCode());
							if(null!=sTradCov&&StrUtil.isNotEmpty(sTradCov.getTradCovCode())){
								long sTradCovCode= Long.parseLong(sTradCov.getTradCovCode())+1;
								String nextCovCodeVal = String.format("%03d", sTradCovCode);
								tempCode=nextCovCodeVal;
							}else{
								long sTradCovCode=Integer.parseInt(tradPlanCov.getCoreCovCode());
								tempCode=sTradCovCode+"001";
							}
						
							TradPlanCov tp = new TradPlanCov();
							tp.setTradPlanCode(tradPlanCov.getTradPlanCode());
							tp.setTradCovCode(tempCode);
//							tp.setCovAmt(formatAmt);
							tp.setIsInputAmount(tradPlanCov.getIsInputAmount());
							if(null!=tradPlanCov.getClaimRate()){
								tp.setClaimRate(tradPlanCov.getClaimRate().divide(new BigDecimal(100)).setScale(5,BigDecimal.ROUND_HALF_UP));
							}
							tp.setNoCalimAmount(tradPlanCov.getNoCalimAmount());
							List<TradPlanCov> pclist = new ArrayList<TradPlanCov>();
							pclist.add(tp);
							gatewayTradPlanCovDao.saveTradPlanCovList(pclist);//新增
							

							//根据传入的责任模板代码，查询对应的责任模板名称
							CoreCoverage cc = coreCoverageDao.queryCoreCoverageByCode(tradPlanCov.getCoreCovCode());	
							List<TradCov> covlist = new ArrayList<TradCov>();
							TradCov tc = new TradCov();
							tc.setCovCode(tradPlanCov.getCoreCovCode());
							tc.setTradCovCode(tempCode);
							tc.setTradCovName(cc.getCovnme());

							covlist.add(tc);
							
							gatewayTradCovDao.saveTradCovList(covlist);	//维护责任表
						}
					}
				}
			}
			//编辑险种时判断是否有 相关的投保方案
			List<TradInsuPlan> tradInsuPlanLists=gatewayTradInsuPlanDao.queryTradInsuPlanByPlanCode(planCovs.get(0).getTradPlanCode());
			if(null!=tradInsuPlanLists&&tradInsuPlanLists.size()>0){
				gatewayChannelInfoConfigService.clearAllCacheByChannelCodeExtend(tradInsuPlanLists);
			}
	}
	@Override
	public void deletePlan(TradProdPlan tradProdPlan,TradPlanBase tradPlanBase,TradPlanCov tradPlanCov,RiskAlgMap riskAlgMap,String isHealthFlag) throws Exception  {
		tradProdPlan = gatewayTradProdPlanDao.queryTradProdPlan(tradProdPlan);
		tradPlanBase=gatewayTradPlanBaseDao.queryTradPlanBase(tradPlanBase);
		
		List<TradPlanCov> tradPlanCovlist= gatewayTradPlanCovDao.queryTradPlanCovList(tradPlanCov);
		
		for (TradPlanCov tradPlanCovs : tradPlanCovlist) {
			TradCov tradcov =gatewayTradCovDao.queryTradCov(tradPlanCovs.getTradCovCode());
			if(null == tradcov){
				continue;
			}else{
				gatewayTradCovDao.deleteTradCovByid(tradcov.getTradCovCode());;
			} 
			gatewayTradPlanCovDao.deleteTradPlanCov(tradPlanCovs);
		}
		//删除算法
		String tradPlanCode = tradProdPlan.getTradPlanCode();
		
		gatewayRiskAlgMapDao.deleteRiskAlgMapByPlanCode(tradPlanCode);
		//added by zzGuo@20160120 for 添加人险产品健康险标识处理 begin
		if("Y".equalsIgnoreCase(isHealthFlag)){
			if(!gatewayTradProdPlanDao.queryIsExistHealthPlan(tradProdPlan)){
				TradProdInst temObj = new TradProdInst();
				temObj.setTradProdCode(tradProdPlan.getTradProdCode());
				temObj.setIsHealthFlag("N");
				tradProdInstDao.updateTradProdInstIsHealthFlag(temObj);
			}
		}
		//added by zzGuo@20160120 for 添加人险产品健康险标识处理 end
		gatewayTradProdPlanDao.deleteTradProdPlan(tradProdPlan);
		gatewayTradPlanBaseDao.deleteTradPlanBase(tradPlanBase);
	}
	
	@Override
	public void deleteTradProdInst(TradProdInst tradProdInst)
			throws Exception {
			
			
			TradPlanBase qtradPlanBase=new TradPlanBase();
			
			TradPlanCov qtradPlanCov=new TradPlanCov();
			
			
			TradProdPlan qtradProdPlan=new TradProdPlan();
			qtradProdPlan.setTradProdCode(tradProdInst.getTradProdCode());
			List<TradProdPlan> tradPlanCodeList=gatewayTradProdPlanDao.queryTradProdPlanList(qtradProdPlan);
			for(TradProdPlan tradProdPlan:tradPlanCodeList){
				String tradPlanCode=tradProdPlan.getTradPlanCode();
				if(StrUtil.isEmpty(tradPlanCode)){
					continue;
				}
				qtradPlanCov.setTradPlanCode(tradPlanCode);
				List<TradPlanCov> tradPlanCovlist= gatewayTradPlanCovDao.queryTradPlanCovList(qtradPlanCov);
				for(TradPlanCov tradPlanCov: tradPlanCovlist){
						TradCov tradcov =gatewayTradCovDao.queryTradCov(tradPlanCov.getTradCovCode());
						if(null == tradcov){
							continue;
						}else{
							gatewayTradCovDao.deleteTradCovByid(tradcov.getTradCovCode());
						} 
						gatewayTradPlanCovDao.deleteTradPlanCov(tradPlanCov);
				}

				qtradPlanBase.setTradPlanCode(tradPlanCode);
					gatewayRiskAlgMapDao.deleteRiskAlgMapByPlanCode(tradPlanCode);
					gatewayTradProdPlanDao.deleteTradProdPlan(tradProdPlan);
					gatewayTradPlanBaseDao.deleteTradPlanBase(qtradPlanBase);
			}
			 
			tradProdInstDao.deleteTradProdInst(tradProdInst);
			tradFeeAdjustConditionDao.deleteTradFeeAdjustConditionsByTradProdCode(tradProdInst);//删除折扣系数关联表信息
	}

	@Override
	public TradProdInst editTradProdInst(TradProdInst tradProdInst) throws Exception {
		tradProdInstDao.updateTradProdInst(tradProdInst);
		tradFeeAdjustConditionDao.deleteTradFeeAdjustConditionsByTradProdCode(tradProdInst);//删除折扣系数关联表信息
		if(tradProdInst.getAdjustStr()!=null&&tradProdInst.getAdjustStr().length()>0){
			JsonObject o = new JsonParser().parse(tradProdInst.getAdjustStr()).getAsJsonObject();
			String adjustId= o.get("adjustId").getAsString();
			JsonArray jary = o.get("adjustList").getAsJsonArray();
			TradFeeAdjustCondition tradFeeAdjustCondition = null;
			if(adjustId!=null&&!"".equals(adjustId)){
				for (JsonElement jsonElement : jary) {
					String peoplenum =jsonElement.getAsJsonObject().get("peoplenum").getAsString();
					String coefficient=jsonElement.getAsJsonObject().get("coefficient").getAsString();
					if(!StrUtil.isEmpty(peoplenum)&&!StrUtil.isEmpty(coefficient)){
						tradFeeAdjustCondition = new TradFeeAdjustCondition();
						tradFeeAdjustCondition.setTradProdCode(tradProdInst.getTradProdCode());
						tradFeeAdjustCondition.setAdjustId(adjustId);
						BigDecimal premRate = BigDecimal.valueOf(Double.parseDouble(coefficient)/100);
						tradFeeAdjustCondition.setCond1(Long.parseLong(peoplenum));
						tradFeeAdjustCondition.setPremRate(premRate.setScale(2, BigDecimal.ROUND_HALF_UP));
						tradFeeAdjustConditionDao.saveTradFeeAdjustCondition(tradFeeAdjustCondition);
					}
					
				}
			}
			
		}
		
		//编辑产品时判断产品是否有方案，如果有就清除缓存
		List<TradInsuPlan> tradInsuPlanLists=gatewayTradInsuPlanDao.queryTradInsuPlanByProdCode(tradProdInst.getTradProdCode());
		if(null!=tradInsuPlanLists&&tradInsuPlanLists.size()>0){
			//效率优化
			gatewayChannelInfoConfigService.clearAllCacheByChannelCodeExtend(tradInsuPlanLists);
		}
		return tradProdInst;
	}

	@Override
	public void editTradPlanBase(TradPlanBase tradplanbase,RiskAlgMap riskAlgMap) throws Exception {
	
		gatewayTradPlanBaseDao.updateTradPlanBaseAmount(tradplanbase);
		gatewayRiskAlgMapDao.updateRiskAlgMap(riskAlgMap);
		
		//编辑险种时判断是否有 相关的投保方案
		List<TradInsuPlan> tradInsuPlanLists=gatewayTradInsuPlanDao.queryTradInsuPlanByPlanCode(tradplanbase.getTradPlanCode());
		if(null!=tradInsuPlanLists&&tradInsuPlanLists.size()>0){
			//效率优化
			gatewayChannelInfoConfigService.clearAllCacheByChannelCodeExtend(tradInsuPlanLists);
		}
	}

	@Override
	public TradPlanBase queryTradPlanBase(TradPlanBase tradplanbase)
			throws Exception {
		// TODO Auto-generated method stub
		return gatewayTradPlanBaseDao.queryTradPlanBase(tradplanbase);
	}

	@Override
	public List<TradPlanCov> queryTradPlanCovList(TradPlanCov tradPlanCov)
			throws SQLException {
		// TODO Auto-generated method stub
		return gatewayTradPlanCovDao.queryTradPlanCovList(tradPlanCov);
	}

	@Override
	public List<TradPlanCov> findState(TradProdInst tradProdInst)
			throws SQLException {
		
		return gatewayTradPlanCovDao.findState(tradProdInst);
	}
	

	@Override
	public TradProdPlan getTradProdPlan(TradProdPlan pp)throws SQLException {
		return gatewayTradProdPlanDao.queryTradProdPlan(pp);
	}

	@Override
	public List<TradProdPlan> queryTradProdPlanList(TradProdPlan qtradProdPlan)
			throws SQLException {
		return gatewayTradProdPlanDao.queryTradProdPlanList(qtradProdPlan);
	}

	public void updatePlanBaseRatio(TradPlanBase base) throws Exception{
		gatewayTradPlanBaseDao.updateTradPlanBase(base);	
	}

	@Override
	public TradPlanBase queryTradPlanBaseByCode(String tradPlanCode)
			throws SQLException {
		return gatewayTradPlanBaseDao.queryTradPlanBaseByCode(tradPlanCode);
	}

	@Override
	public void updateTradProdInst(TradProdInst tradProdInst) throws Exception {
		tradProdInstDao.updateTradProdInst(tradProdInst);
	}

	/**
	 * 查询折扣系数选项
	 * wangyan 
	 * 2015年6月3日18:08:44
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> getDiscountTypes() throws Exception {
		
		return this.tradProdInstDao.queryList("select ADJUST_ID as adjustid,ADJUST_DESC as adjustdesc  from t_trad_fee_adjust", null);
	}
	
	/**
	 * 查询子险种算法
	 * wangyan 
	 * 2015年6月5日11:29:53
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List<TradItemCal> getgetChildCals(TradItemCal trad) throws Exception {
		
		return this.tradProdInstDao.queryTradItemCal(trad);
	}

	@Override
	public List<TradInsuPlan> queryTradInsuPlans(TradProdInst tradProdInst)
			throws Exception {
		return gatewayTradInsuPlanDao.queryTradInsuPlanByProdCode(tradProdInst.getTradProdCode());
	}

	@Override
	public boolean isExistProdPlan(String tradProdCode) throws Exception {
		boolean flag= false;
		List<TradInsuPlan> list = gatewayTradInsuPlanDao.queryTradInsuPlanByProdCode(tradProdCode);
		if(list!=null&&!list.isEmpty()){
			flag = true;
		}
		return flag;
	}

	@Override
	public List<TradPlanBase> queryTradPlanBaseInfoByPlanNo(String polNo) throws SQLException {
		return gatewayTradPlanBaseDao.queryTradPlanBaseInfoByPlanNo(polNo);
	}
}
